/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */

var dataID = $.getUrlVar("id");

var info=new Vue({
  el:"#app",
  data:{
    info:{},
    addShow: false,
    type: ''  ,//buy表示立即兑换，cart加入购物车
    num: 1
  },
  compiled:function(){
    var _self = this;
    
  },
  created:function(){
    var dtd = $.Deferred();
    var _self = this;

    //商品详情
    var data = {
        goodsID: dataID,
    };
    data = Common.jsonEncode(data);
    Common.ajaxCall({ url: 'user_getGoodsDetail', datatype: 'json', type: 'post', data: data}, function (data) {
        if(data.error_code==0){
            var obj = data.data[0];
            _self.info = obj;
        }else{
            alert(data.msg);
        }
    });

  },
  methods:{
    //商品兑换
    addExchange:function(type){
        this.add();
        var d = new Date();
        //$.setCookie('ordertime', d.getTime()); //设置订单标识
        var data = {
            goodsID: dataID,
            number : $("#count").val()
        };
        data = Common.jsonEncode(data);
        if(type === 'buy'){
          window.location.href = "orderSubmit.html?buyNow=true&goodsCollection="+ dataID + "," + $("#count").val()+"&order=" + d.getTime();
        }else{
          Common.ajaxCall({ url: 'user_addCart', datatype: 'json', type: 'post', data: data}, function (data) {
            if(data.error_code==0){
                var pic = "success.png";
                alert({
                    text: '<span class="db padTB40 font26">已添加至购物车</span>',
                    close: false,
                    kind: 2,
                    onCancel: '关闭',
                    onOk: '购物车',
                    okEvent:function(){
                      window.location.href = "cart.html";
                    }
                });
            }else{
                alert(data.msg);
            }
          });
        }
    },
    changeNum:function(item,opt){
      if(opt>0){
        if(this.num==item.stock) return;
        this.num++;
      }else{
        if(this.num==1) return;
        this.num--;
      }
    },
    add:function(type){
        if(type){
          this.type = type;
        }
        if(this.info.personGold<this.info.price){
            return;
        }
        if(this.addShow == false){
            this.addShow = true;
        }else{
            this.addShow = false;
        }
    },
    loadPhoto:function(){
        // 比例
        myJS.imgScale('.j_3', '3', '1');
        myJS.imgScale('.j_1', '1', '1');
        myJS.imgScale('.j_4', '4', '3');
        myJS.ImgFull('.loadImg','images/liby.png');
    }
  },
  watch:{
      "info": function(){
          this.loadPhoto();
      },
      "addShow": function(){
          this.loadPhoto();
      }
  }
});



